<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/19
 * Time: 11:58
 */

header("Content-type: text/html; charset=utf-8");

$conn = mysqli_connect('localhost', 'root', 'root', 'test');
if (!$conn) {
    die('链接失败' . mysqli_error($conn));
}




if (isset($_GET['page'])) {
    $page = $_GET['page'];
} else {
    $page = 1;
}
//页面显示2行
$page_size = 2;
$sql = 'select * from user';
$result = mysqli_query($conn, $sql);

$total = mysqli_num_rows($result);
if ($total) {
    if ($total < $page_size) {
        $page_count = 1;
    } else if ($total % $page_size) {
        $page_count = (int)($total / $page_size) + 1;
    } else {
        $page_count = $total / $page_size;
    }
} else {
    $page_count = 0;
}

$turn_page = '';
if ($page === 1) {
    $turn_page .= '首页|上一页|';
} else {
    $turn_page .= '<a href="pagingData.php?page=1">首页</a>|<a href="pagingData.php?page=' . ($page - 1) . '">上一页</a>|';
}
if ($page === $page_count || $page_count === 0) {
    $turn_page .= '下一页|尾页';
} else {
    $turn_page .= '<a href=pagingData.php?page=' . ($page + 1) . '>下一页</a>|<a href=pagingData.php?page=' . $page_count . '>尾页</a>';
}
$sql = 'select * from user limit ' . ($page - 1) * $page_size . ',' . $page_size;
$result = mysqli_query($conn, $sql) OR die("<br/>ERROR: <b>" . mysqli_error() . "</b><br/>产生问题的SQL：<br/>" . $sql);
?>

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数据库分页列表</title>
    <link rel="stylesheet" href="../css/data.css">
    <style>
        body{
            padding-top: 8%;
        }
    </style>
</head>
<body>
<table border="1" cellspacing="1" cellpadding="0" bgcolor="#7b7b84" class="data-table">
    <tr>
        <th>id</th>
        <th>用户名</th>
        <th>城市</th>
        <th>创建时间</th>
        <th>操作</th>
    </tr>
    <?php
    if ($num = mysqli_num_rows($result)) {
        while ($row = mysqli_fetch_assoc($result)) {
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['username']; ?></td>
                <td><?php echo $row['city']; ?></td>
                <td><?php echo $row['add_time']; ?></td>
                <td><a href="pagingData1.php?id=<?php echo $row['id']; ?>" class="del">删除</a></td>
            </tr>
            <?php
        }
    }
    ?>

</table>

<?php  echo "<div class='page-box'>".$turn_page."</div>";
mysqli_close($conn); ?>


<script>
    var del = document.getElementsByClassName("del");
    for (var i = 0; i < del.length; i++) {
        del[i].onclick = function () {
            if (confirm('您确定要删除用户信息吗？')) {
                return true;
            } else {
                return false;
            }
        }
    }
</script>
</body>
</html>





